<template>
    <view>
        <!-- 导航 -->
        <view class="nav">
            <block v-for="(item, index) in nav_status_list" :key="item.key">
                <view v-if="nav_status_index == index" class="item fl tc cr-main" :data-index="index" @click="nav_event">{{ item.name }}</view>

                <view v-else class="item fl tc" :data-index="index" @click="nav_event">{{ item.name }}</view>
            </block>
        </view>

        <!-- 订单列表 -->
		<scroll-view :scroll-y="true" class="scroll-box" @scrolltolower="scroll_lower" lower-threshold="30">
			<view v-if="data_list.length > 0" class="padding-horizontal-main padding-top-main">
                <view class="list-item padding-horizontal-main padding-top-main border-radius-main bg-white oh spacing-mb" v-for="(item, index) in data_list" :key="index">
                    <view class="item-base oh br-b">
                        <text class="cr-666">{{ item.add_time }}</text>
                        <text class="fr cr-main">{{ item.status_name }}</text>
                    </view>

                    <view class="goods-item oh" v-for="(detail, index1) in item.items" :key="index1">
                        <navigator :url="'/pages/fcc/scorestore/orderdetail/orderdetail?id=' + item.id" hover-class="none">
                            <image class="goods-image fl" :src="detail.images" mode="aspectFill" />
                            <view class="goods-base">
                                <view class="goods-title multi-text">{{ detail.title }}</view>
                                <block v-if="detail.spec != null">
                                    <view class="goods-spec cr-888" v-for="(spec, index2) in detail.spec" :key="index2">{{ spec.type }}:{{ spec.value }}</view>
                                </block>
                            </view>
                            <view class="oh goods-price">
                                <text class="sales-price">{{ detail.price }}积分</text>
                                <text v-if="detail.original_price > 0" class="original-price">￥{{ detail.original_price }}</text>
                                <text class="buy-number">x{{ detail.buy_number }}</text>
                            </view>
                        </navigator>
                    </view>

                    <view class="item-describe tr cr-666">{{ item.describe }}</view>

                    <view v-if="item.status <= 1 || item.status == 3 || ((item.express_data || null) != null && item.express_data.length > 0)" class="item-operation tr br-t">
                    	<button v-if="wuliu_type == 1 && (item.express_data || null) != null && item.express_data.length > 0" class="round bg-white cr-main br-main" type="default" size="mini" @tap="url_event" :data-value="'/pages/fcc/user-order-express/user-order-express?order_id=' + item.id + '&business_type=scorestoreorder'" hover-class="none">{{$t('orderallot-list.orderallot-list.w2t242')}}</button>
                    	<button v-if="wuliu_type == 2 && (item.express_data || null) != null && item.express_data.length > 0" class="round bg-white cr-main br-main" type="default" size="mini" @tap="url_event" :data-value="'/pages/fcc/aliquanqiuexpress/aliquanqiuexpress?order_id=' + item.id + '&business_type=scorestoreorder'" hover-class="none">{{$t('orderallot-list.orderallot-list.w2t242')}}</button>
                    
                        <button
                            v-if="item.status <= 1"
                            class="round bg-white cr-yellow br-yellow margin-bottom-main"
                            type="default"
                            size="mini"
                            @click="cancel_event"
                            :data-value="item.id"
                            :data-index="index"
                            hover-class="none"
                        >
                            {{$t('common.cancel')}}
                        </button>
                        <button
                            v-if="item.status == 3"
                            class="round bg-white cr-green br-green margin-bottom-main"
                            type="default"
                            size="mini"
                            @click="collect_event"
                            :data-value="item.id"
                            :data-index="index"
                            hover-class="none"
                        >
                            {{$t('orderallot-list.orderallot-list.w2w2w4')}}
                        </button>
                    </view>
                </view>
                <component-bottom-line :prop-status="data_bottom_line_status"></component-bottom-line>
            </view>
			<view v-else>
			    <component-no-data :prop-status="data_list_loding_status" :prop-msg="data_list_loding_msg"></component-no-data>
			</view>
		</scroll-view>

        <!-- 支付方式 popup -->
        <component-popup :prop-show="is_show_payment_popup" prop-position="bottom" @onclose="payment_popup_event_close">
            <view v-if="payment_list.length > 0" class="payment-list oh bg-white">
                <view class="item tc fl" v-for="(item, index) in payment_list" :key="index">
                    <view class="item-content br" :data-value="item.id" @click="popup_payment_event">
                        <image v-if="(item.logo || null) != null" class="icon" :src="item.logo" mode="widthFix" />
                        <text>{{ item.name }}</text>
                    </view>
                </view>
            </view>
            <view v-else class="payment-list oh bg-white tc cr-888">没有支付方式</view>
        </component-popup>

        <!-- 结尾 -->
        <component-footer :prop-status="3"></component-footer>
    </view>
</template>

<script>
import componentPopup from '@/components/popup/popup';
import componentIconNav from '@/components/icon-nav/icon-nav';
import componentBanner from '@/components/slider/slider';
import componentFooter from "@/pages/fcc/scorestore/public/footer";
import componentNoData from "@/components/no-data/no-data";
import componentBottomLine from "@/components/bottom-line/bottom-line";
import componentOnlineService from "@/components/online-service/online-service";
const app = getApp();
export default {
    components: {
        componentPopup,
		componentIconNav,
		componentBanner,
		componentFooter,
		componentNoData,
		componentBottomLine,
		componentOnlineService
    },
    data() {
        return {
            data_list: [],
            data_page_total: 0,
            data_page: 1,
            data_list_loding_status: 1,
            data_bottom_line_status: false,
            params: null,
            input_keyword_value: '',
            load_status: 0,
            is_show_payment_popup: false,
            payment_list: [],
            payment_id: 0,
            temp_pay_value: 0,
            temp_pay_index: 0,

            nav_status_list: [
                {
                    name: '全部',
                    value: '-1'
                },
                {
                    name: '待支付',
                    value: '1'
                },
                {
                    name: '待发货',
                    value: '2'
                },
                {
                    name: '待收货',
                    value: '3'
                },
                {
                    name: '已完成',
                    value: '4'
                },
                {
                    name: '已失效',
                    value: '5,6'
                }
            ],

            nav_status_index: 0,
            data_total: '',

            detail: {
                key: '',
                images: '',
                title: '',
                spec: '',
                price: '',
                original_price: 0,
                buy_number: ''
            },

            spec: {
                key: '',
                type: '',
                value: ''
            },

            data_list_loding_msg: '',
			wuliu_type: app.globalData.data.wuliu_type || 1,
        };
    },
    onLoad(params) {
        // 是否指定状态
        var nav_status_index = 0;

        if ((params.status || null) != null) {
            for (var i in this.nav_status_list) {
                if (this.nav_status_list[i]['value'] == params.status) {
                    nav_status_index = i;
                    break;
                }
            }
        }

        this.setData({
            params: params,
            nav_status_index: nav_status_index
        });
        this.init();
    },
    onShow() {
		app.globalData.page_event_onshow_handle();
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            data_page: 1
        });
        this.get_data_list(1);
    },
    methods: {
        init() {
            var user = app.globalData.get_user_info(this, 'init');

            if (user != false) {
                // 获取数据
                this.get_data_list();
            } else {
                this.setData({
                    data_list_loding_status: 0,
                    data_bottom_line_status: false
                });
            }
        },

        // 输入框事件
        input_event(e) {
            this.setData({
                input_keyword_value: e.detail.value
            });
        },

        // 获取数据
        get_data_list(is_mandatory) {
            // 分页是否还有数据
            if ((is_mandatory || 0) == 0) {
                if (this.data_bottom_line_status == true) {
                    return false;
                }
            } // 加载loding

            uni.showLoading({
                title: this.$t('common.loading_in_text'),
            });
            this.setData({
                data_list_loding_status: 1
            }); // 参数

            var order_status = (this.nav_status_list[this.nav_status_index] || null) == null ? -1 : this.nav_status_list[this.nav_status_index]['value']; // 获取数据

            uni.request({
                url: app.globalData.get_request_url('orderlist', 'index', 'scorestore'),
                method: 'POST',
                data: {
                    page: this.data_page,
                    keywords: this.input_keyword_value || '',
                    status: order_status,
                    is_more: 1
                },
                dataType: 'json',
                success: (res) => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();

                    if (res.data.code == 0) {
                        if (res.data.data.data.length > 0) {
                            if (this.data_page <= 1) {
                                var temp_data_list = res.data.data.data; // 下订单支付处理

                                if (this.load_status == 0) {
                                    if ((this.params.is_pay || 0) == 1 && (this.params.order_id || 0) != 0) {
                                        for (var i in temp_data_list) {
                                            if (this.params.order_id == temp_data_list[i]['id']) {
                                                this.pay_handle(this.params.order_id, i);
                                                break;
                                            }
                                        }
                                    }
                                }
                            } else {
                                var temp_data_list = this.data_list;
                                var temp_data = res.data.data.data;

                                for (var i in temp_data) {
                                    temp_data_list.push(temp_data[i]);
                                }
                            }

                            this.setData({
                                data_list: temp_data_list,
                                data_total: res.data.data.total,
                                data_page_total: res.data.data.page_total,
                                data_list_loding_status: 3,
                                data_page: this.data_page + 1,
                                load_status: 1,
                                payment_list: res.data.data.payment_list || []
                            }); // 是否还有数据

                            if (this.data_page > 1 && this.data_page > this.data_page_total) {
                                this.setData({
                                    data_bottom_line_status: true
                                });
                            } else {
                                this.setData({
                                    data_bottom_line_status: false
                                });
                            }
                        } else {
                            this.setData({
                                data_list_loding_status: 0,
                                load_status: 1,
                                data_list: [],
                                data_bottom_line_status: false
                            });
                        }
                    } else {
                        this.setData({
                            data_list_loding_status: 0,
                            load_status: 1
                        });
                        app.globalData.showToast(res.data.msg);
                    }
                },
                fail: () => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    this.setData({
                        data_list_loding_status: 2,
                        load_status: 1
                    });
                    app.globalData.showToast(this.$t('common.internet_error_tips'));
                }
            });
        },

        // 滚动加载
        scroll_lower(e) {
            this.get_data_list();
        },

        // 支付
        pay_event(e) {
            this.setData({
                is_show_payment_popup: true,
                temp_pay_value: e.currentTarget.dataset.value,
                temp_pay_index: e.currentTarget.dataset.index
            });
        },

        // 支付弹窗关闭
        payment_popup_event_close(e) {
            this.setData({
                is_show_payment_popup: false
            });
        },

        // 支付弹窗发起支付
        popup_payment_event(e) {
            var payment_id = e.currentTarget.dataset.value || 0;
            this.setData({
                payment_id: payment_id
            });
            this.payment_popup_event_close();
            this.pay_handle(this.temp_pay_value, this.temp_pay_index);
        },

        // 支付方法
        pay_handle(order_id, index) {
            var $this = this; // 加载loding

            uni.showLoading({
                title: '请求中...'
            });
            uni.request({
                url: app.globalData.get_request_url('pay', 'index', 'scorestore'),
                method: 'POST',
                data: {
                    id: order_id,
                    payment_id: this.payment_id
                },
                dataType: 'json',
                success: (res) => {
                    uni.hideLoading();
                    //console.log('--------------------------------------------------提交后获取结果：' + JSON.stringify(res));

                    if (res.data.code == 0) {
                        var temp_data_list = this.data_list;
						if(temp_data_list[index]['order_model'] == 1){
							temp_data_list[index]['status'] = 3;
							temp_data_list[index]['status_name'] = '待收货';
						}else{
							temp_data_list[index]['status'] = 2;
							temp_data_list[index]['status_name'] = (temp_data_list[index]['order_model'] == 2) ? '待取货' : '待发货';
						}
                        this.setData({
                            data_list: temp_data_list
                        });
                        app.globalData.showToast('兑换成功', 'success');
                    } else {
                        app.globalData.showToast(res.data.msg);
                    }
                },
                fail: () => {
                    uni.hideLoading();
                    app.globalData.showToast(this.$t('common.internet_error_tips'));
                }
            });
        },

        // 取消
        cancel_event(e) {
            uni.showModal({
                title: '温馨提示',
                content: '取消后不可恢复，确定继续吗?',
                confirmText: '确认',
                cancelText: '不了',
                success: (result) => {
                    if (result.confirm) {
                        // 参数
                        var id = e.currentTarget.dataset.value;
                        var index = e.currentTarget.dataset.index; // 加载loding

                        uni.showLoading({
                            title: '处理中...'
                        });
                        uni.request({
                            url: app.globalData.get_request_url('cancel', 'index', 'scorestore'),
                            method: 'POST',
                            data: {
                                id: id
                            },
                            dataType: 'json',
                            success: (res) => {
                                uni.hideLoading();

                                if (res.data.code == 0) {
                                    var temp_data_list = this.data_list;
                                    temp_data_list[index]['status'] = 5;
                                    temp_data_list[index]['status_name'] = '已取消';
                                    this.setData({
                                        data_list: temp_data_list
                                    });
                                    app.globalData.showToast(res.data.msg, 'success');
                                } else {
                                    app.globalData.showToast(res.data.msg);
                                }
                            },
                            fail: () => {
                                uni.hideLoading();
                                app.globalData.showToast(this.$t('common.internet_error_tips'));
                            }
                        });
                    }
                }
            });
        },

        // 收货
        collect_event(e) {
            uni.showModal({
                title: '温馨提示',
                content: '请确认已收到货物或已完成，操作后不可恢复，确定继续吗?',
                confirmText: '确认',
                cancelText: '不了',
                success: (result) => {
                    if (result.confirm) {
                        // 参数
                        var id = e.currentTarget.dataset.value;
                        var index = e.currentTarget.dataset.index; // 加载loding

                        uni.showLoading({
                            title: '处理中...'
                        });
                        uni.request({
                            url: app.globalData.get_request_url('collect', 'index', 'scorestore'),
                            method: 'POST',
                            data: {
                                id: id
                            },
                            dataType: 'json',
                            success: (res) => {
                                uni.hideLoading();

                                if (res.data.code == 0) {
                                    var temp_data_list = this.data_list;
                                    temp_data_list[index]['status'] = 4;
                                    temp_data_list[index]['status_name'] = '已完成';
                                    this.setData({
                                        data_list: temp_data_list
                                    });
                                    app.globalData.showToast(res.data.msg, 'success');
                                } else {
                                    app.globalData.showToast(res.data.msg);
                                }
                            },
                            fail: () => {
                                uni.hideLoading();
                                app.globalData.showToast(this.$t('common.internet_error_tips'));
                            }
                        });
                    }
                }
            });
        },

        // 导航事件
        navigation_event(e) {
            app.globalData.operation_event(e);
        },

        // 催催
        rush_event(e) {
            app.globalData.showToast('催促成功', 'success');
        },
		
		// url事件
		url_event(e) {
		    app.globalData.url_event(e);
		},

        // 导航事件
        nav_event(e) {
            this.setData({
                nav_status_index: e.currentTarget.dataset.index || 0,
                data_page: 1
            });
            this.get_data_list(1);
        }
    }
};
</script>
<style>
@import './order.css';
</style>
